library(ggplot2)
library(ggthemes)
library(reshape2)


# 1. Density ----
ggplot(diamonds, aes(x = price)) + geom_density(colour = "steelblue")

ggplot(diamonds, aes(x = price, colour = clarity)) + 
  geom_density()

ggplot(diamonds, aes(x = price, fill = clarity, alpha = 1/10)) + 
  geom_density()

set.seed(1234)
df <- data.frame(sex = factor(rep(c("F", "M"), each = 200)),
                 weight = round(c(
                     rnorm(200, mean = 55, sd = 5),
                     rnorm(200, mean = 65, sd = 5)
                 )))
head(df)

# basic density ----
p <- ggplot(df, aes(x = weight)) +
    geom_density()
p

# add mean line
p + geom_vline(aes(xintercept = mean(weight)),
               color = "steelblue", linetype = "dashed", size = 1)

# change line color and fill color
ggplot(df, aes(weight)) +
    geom_density(color = "darkblue", fill = "lightblue")

# change line type
ggplot(df, aes(weight)) +
    geom_density(linetype = "dashed")

# Density plot by groups ----

library(dplyr)
mu <- df %>% group_by(sex) %>% summarise(grp.mean = mean(weight))
head(mu)

# change density plot line colors by groups
ggplot(df, aes(weight, color = sex)) +
    geom_density()

# add mean line
p <- ggplot(df, aes(weight, color = sex)) +
    geom_density() +
    geom_vline(data = mu, aes(xintercept = grp.mean, color = sex),
               linetype = "dashed")
p

# Use custom color palettes
p + scale_color_manual(values = c("#999999", "#E69F00", "#56B4E9"))
# Use brewer color palettes
p + scale_color_brewer(palette = "Dark2")
# Use grey scale
p + scale_color_grey() + theme_classic()

# change fill colors
ggplot(df, aes(weight, fill = sex)) +
    geom_density()
p <- ggplot(df, aes(weight, fill = sex)) +
    geom_density(alpha = 0.4)
p + geom_vline(data = mu,
               aes(xintercept = grp.mean, color = sex),
               linetype = "dashed")

# Use custom color palettes
p + scale_fill_manual(values = c("#999999", "#E69F00", "#56B4E9"))
# use brewer color palettes
p + scale_fill_brewer(palette = "Dark2")
# Use grey scale
p + scale_fill_grey() + theme_classic()

# Legned ----
p + theme(legend.position = "top")
p + theme(legend.position = "bottom")
p + theme(legend.position = "none")  # remove legend

# Histogram combined density ----
ggplot(df, aes(weight)) +
    geom_histogram(aes(y = ..density..), color = "black", fill = "white") +
    geom_density(alpha = 0.2, fill = "#FF6666")
# colors by groups
ggplot(df, aes(weight, color = sex, fill = sex)) +
    geom_histogram(aes(y = ..density..), alpha = 0.5, position = "identity") +
    geom_density(alpha = 0.2)

# ggpubr::ggdensity()
library(ggpubr)
ggdensity(df, x = "weight", add = "mean", rug = TRUE,
          color = "sex", fill = "sex",
          palette = c("#00AFBB", "#E7B800"))

# Facets ----
p <- ggplot(df, aes(weight)) +
    geom_density() +
    facet_grid(sex ~ .)
p

ggdensity(df, x = "weight", facet.by = "sex")

p + geom_vline(data = mu, aes(xintercept = grp.mean),
               color = "red", linetype = "dashed")

# Customized ----
ggplot(df, aes(weight, fill = sex)) +
    geom_density(fill = "gray") +
    geom_vline(aes(xintercept = mean(weight)),
               color = "steelblue", linetype = "dashed") +
    labs(title = "Weight density curve", 
         x = "Weight (kg)", y = "Density") +
    theme_classic()
# change line color by groups
p <- ggplot(df, aes(weight, color = sex)) +
    geom_density() +
    geom_vline(data = mu, aes(xintercept = grp.mean, color = sex),
               linetype = "dashed") +
    labs(title = "Weight density curve", 
         x = "Weight (kg)", y = "Density")
p + scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9")) +
    theme_classic()

# Continuous colors
p + scale_color_brewer(palette = "Paired") + theme_classic()
# Discrete colors
p + scale_color_brewer(palette = "Dark2") + theme_minimal()
# Gradient colors
p + scale_color_brewer(palette = "Accent") + theme_minimal()

# ECDF ----
# Empirical cumulative density function
df <- data.frame(height = round(rnorm(200, mean = 60, sd = 15)))
head(df)

ggplot(df, aes(height)) + stat_ecdf(geom = "point")

ggplot(df, aes(height)) + stat_ecdf(geom = "step") +
    labs(title = "Empirical Cumulative \n Density Function",
         y = "F(height)", x = "Height in inch") +
    theme_classic()

# qq-plot ----
mtcars$cyl <- as.factor(mtcars$cyl)
head(mtcars)

# solution1
qplot(sample = mpg, data = mtcars)
# solution2
ggplot(mtcars, aes(sample = mpg)) + stat_qq()

# change point shape by groups
p <- qplot(sample = mpg, data = mtcars, shape = cyl)
p
# change point shape manually
p + scale_shape_manual(values = c(1, 17, 19))

# change qq plot colors by groups
p <- qplot(sample = mpg, data = mtcars, color = cyl)
p

# Use custom color palettes
p + scale_color_manual(values = c("#999999", "#E69F00", "#56B4E9"))
# Use brewer color palettes
p + scale_color_brewer(palette = "Dark2")
# Use grey scale
p + scale_color_grey() + theme_classic()

# change legend position
p + theme(legend.position = "top")
p + theme(legend.position = "bottom")
p + theme(legend.position = "none") # Remove legend

# customized qq plot
qplot(sample = mpg, data = mtcars) +
    labs(title = "Miles per gallon \n according to the weight",
         y = "Miles/(US) gallon") +
    theme_classic()
# Change color/shape by groups
p <- qplot(
    sample = mpg,
    data = mtcars,
    color = cyl,
    shape = cyl
) +
    labs(title = "Miles per gallon \n according to the weight",
         y = "Miles/(US) gallon")
p + theme_classic()
# Continuous colors
p + scale_color_brewer(palette = "Blues") + theme_classic()
# Discrete colors
p + scale_color_brewer(palette = "Dark2") + theme_minimal()
# Gradient colors
p + scale_color_brewer(palette = "RdBu")
